/**************************************************************************
***************************************************************************
	GameTimer.cpp

	The game timer class is responsible for managing our frame rate and
	ensuring that the game will be ran at a constant playable speed on 
	various machines.
***************************************************************************
***************************************************************************/
#pragma once

#include "GameTimer.H"

GameTimer::GameTimer()
{
	stop( );
};

bool GameTimer::getEnabled ( )
{
	return isEnabled;
}

int GameTimer::getMSElapsed( )
{
	if( starttime > clock( ) || starttime < 0 )
	{
		this->restart( ); //Prevent wrapping and negative values
		return 0;
	}

	
	this->elapsed = ( clock( ) - starttime ) / ( CLOCKS_PER_SEC / 1000 );
	return elapsed;
};

int GameTimer::getSecElapsed( )
{
	if( starttime > clock( ) || starttime < 0 )
	{
		this->restart( ); //Prevent wrapping and negative values
		return 0;
	}

	elapsed = ( clock( ) - starttime ) / CLOCKS_PER_SEC;
	return elapsed;
};

void GameTimer::restart( )
{
	starttime = clock( );
};

void GameTimer::start( )
{
	if ( isEnabled == false )
	{
		isEnabled = true;
		starttime = clock( );
	}
};

void GameTimer::stop( )
{
	isEnabled = false;
};

void GameTimer::waitMS( int milliseconds )
{
	/*clock_t endwait;
	endwait = clock( ) + ( milliseconds * ( CLOCKS_PER_SEC / 1000 ) );
	while( clock( ) < endwait) {}*/
	// Maintain above for reference
	Sleep( milliseconds );
};

void GameTimer::waitSec( int seconds )
{
	/*clock_t endwait;
	endwait = clock( ) + seconds * CLOCKS_PER_SEC;
	while( clock( ) < endwait) { }*/
	// Maintain above for reference
	Sleep( seconds * 1000 );
};

GameTimer::~GameTimer( )
{
	//
};